﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using LUStationery.Model;
using LUStationery.DAO;
namespace LUStationery.Controller
{

    public class ItemListControl
    {
        public ItemListControl()
        {

        }
        public ItemList getItemListByCategory(string category)
        {
            return getItemsByCategory(category);
        }

        public ItemList getItemList()
        {
            return getItems();
        }

        //// ZHONG YUE O(∩_∩)O~ I add static method here because it needed!!!\\\\
        public ItemList getItems()
        {
            ItemDAO iDAO = new ItemDAO();
            IDataReader idr = iDAO.getItems();
            return ItemListControl.convertIdrToItemList(idr);
        }
        //// ZHONG YUE O(∩_∩)O~ I add static method here because it needed!!!\\\\
        public ItemList getUnSafeStockItems()
        {
            ItemDAO iDAO = new ItemDAO();
            IDataReader idr = iDAO.getUnSafeStockItems();
            return ItemListControl.convertIdrToItemList(idr);
        }
        //// ZHONG YUE O(∩_∩)O~ I add static method here because it needed!!!\\\\
        public ItemList getItemsBySupplierAndCategory(string suppiler, string category)
        {
            ItemDAO iDAO = new ItemDAO();
            IDataReader idr = iDAO.getItemsBySupplierAndCategory(suppiler, category);
            return ItemListControl.convertIdrToItemList(idr);
        }

        //// ZHONG YUE O(∩_∩)O~ I add static method here because it needed!!!\\\\
        public Item getItemByItemCode(string itemCode)
        {
            ItemDAO iDAO = new ItemDAO();
            IDataReader idr = iDAO.getItemByItemCode(itemCode);
            return ItemListControl.convertIdrToItem(idr);
        }

        //// ZHONG YUE O(∩_∩)O~ I add static method here because it needed!!!\\\\
        public ItemList getItemsByCategory(string category)
        {
            ItemDAO iDAO = new ItemDAO();
            IDataReader idr = iDAO.getItemsByCategory(category);
            return ItemListControl.convertIdrToItemList(idr);
        }

        protected static ItemList convertIdrToItemList(IDataReader idr)
        {
            ItemList iList = new ItemList();
            while (idr.Read())
            {
                Item i = new Item();
                i.ItemNumber = Convert.ToString(idr["item_code"]);
                i.ItemDesc = Convert.ToString(idr["item_description"]);
                i.Category = Convert.ToString(idr["category"]);
                i.Bin = Convert.ToString(idr["bin"]);
                i.UOM = Convert.ToString(idr["uom"]);
                i.Balance = Convert.ToInt32(idr["balance"]);
                i.ReorderLevel = Convert.ToInt32(idr["reorder_level"]);
                i.OnOrderQty = Convert.ToInt32(idr["onorder_qty"]);
                iList.Add(i);
            }
            return iList;
        }

        protected static Item convertIdrToItem(IDataReader idr)
        {
            Item i = new Item();
            while (idr.Read())
            {
                i.ItemNumber = Convert.ToString(idr["item_code"]);
                i.ItemDesc = Convert.ToString(idr["item_description"]);
                i.Category = Convert.ToString(idr["category"]);
                i.Bin = Convert.ToString(idr["bin"]);
                i.UOM = Convert.ToString(idr["uom"]);
                i.Balance = Convert.ToInt32(idr["balance"]);
                i.ReorderLevel = Convert.ToInt32(idr["reorder_level"]);
                i.OnOrderQty = Convert.ToInt32(idr["onorder_qty"]);
            }
            return i;
        }
     
    }
}